<?php

namespace app\index\controller;

use think\Controller;

class Base extends Controller {
	protected $uid = 0;
	protected $lang = '';
	protected $member = '';
	protected $website;
	public function initialize() {
		$lang = $this->request->param ( 'lang' );
		$lang ? cookie ( 'think_var', $lang ) : '';
		if ($lang != '') {
			$url = GetCurrentHost () . $_SERVER ['QUERY_STRING'];
			$url = str_replace ( 'lang', 'jumplang', $url );
			$url = str_replace ( 's=/', '', $url );
			$this->redirect ( $url );
		}
		$this->lang = cookie ( 'think_var' );
		if (session ( 'uid' ) > 0) {
			$this->uid = session ( 'uid' );
		} else {
			$pub = array ('Index','Okx' );
			if (! in_array ( $this->request->controller (), $pub )) {
				$this->redirect ( url ( 'Passport/logout' ) );
				exit ();
			}
		}
		$website = model ( 'Website' )->getAll ();
		if ($website ['status'] == 0)
			exit ();
		$ipforbid = model ( 'Website' )->ipforbidInfo ( array ([ 'ip','=',get_client_ip () ] ) );
		if (count ( $ipforbid ) > 0)
			exit ();
		if ($this->uid > 0) {
			$member = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$this->uid ] ) );
			if ($member ['role_id'] > 0) {
				$role = model ( 'Member' )->roleInfo ( array ([ 'id','=',$member ['role_id'] ] ) );
				$member ['role_name'] = $role ['title'];
				$member ['deposit'] = $role ['deposit'];
				// 当天额度
				$map [] = [ 'receive_uid','=',$this->uid ];
				$map [] = [ 'create_time','>=',strtotime ( date ( 'Y-m-d 00:00:00' ) ) ];
				$map [] = [ 'create_time','<=',strtotime ( date ( 'Y-m-d 23:59:59' ) ) ];
				$num = model ( 'Order' )->getMoney ( $map );
				$member ['daily_pay'] = $role ['daily_pay'] - $num;
				unset ( $map );
				// 单月额度
				$t = date ( "t", strtotime ( date ( 'Y-m-d' ) ) );
				$map [] = [ 'receive_uid','=',$this->uid ];
				$map [] = [ 'create_time','>=',strtotime ( date ( "Y-m-01 00:00:00" ) ) ];
				$map [] = [ 'create_time','<=',strtotime ( date ( "Y-m-$t 23:59:59" ) ) + 24 * 60 * 60 * 100 ];
				$num = model ( 'Order' )->getMoney ( $map );
				$member ['month_pay'] = $role ['month_pay'] - $num;
			}
			// $withdraw = model ( 'Withdraw' )->getInfo ( array ([ 'w.user_id','=',$this->uid ],[ 'w.status','=',3 ] ) );
			$withdraw = model ( 'Member' )->getInfo ( array ([ 'm.user_id','=',$this->uid ] ) );
			if ($withdraw ['status'] == 0)
				$member ['freeze'] = 1; // 冻结
			else
				$member ['freeze'] = 0;
			$this->member = $member;
		}
		$this->website = $website;
		$this->assign ( 'member', $member );
		$this->assign ( 'website', $website );
	}
}